package com.code.module.permission.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class PermissionFilter implements Filter  {
	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpSession session = request.getSession();
		
		if (null == session.getAttribute("SYS_USER")) {
			if (request.getRequestURI().indexOf(request.getContextPath() + "/login.jsp") >= 0 ||
					request.getRequestURI().indexOf(request.getContextPath() + "/index.jsp") >= 0 ||
					request.getRequestURI().indexOf(request.getContextPath() + "/permissionAction!login.action") >= 0) {
				
				arg2.doFilter(arg0, arg1);
				
			} else {
				request.getRequestDispatcher("/index.jsp").forward(arg0, arg1);
			}
			
		} else {
			arg2.doFilter(arg0, arg1);
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
	}
	
}
